package com.jumi.microservice.stock.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.stock.domain.*;
import com.jumi.microservice.stock.service.StockTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @ClassName StockTransferController
 * @Author Abel Yang
 * @Date 2020/9/11/011 14:14
 **/
@Api(tags = {"【仓库管理】调拨(仓库间库存调换)相关接口"})
@RestController
@RequestMapping("/supply/transfer")
public class StockTransferController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(StockTransferController.class);

    @Resource
    private StockTransferService stockTransferService;

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "stockTransferApplyDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockTransferApplyDTO")})
    @ApiOperation("添加申请调货")
    @PostMapping("/apply")
    @WebLog(description = "添加申请调货")
    public ResponseResult addTransfer(@Validated @RequestBody StockTransferApplyDTO apply, HttpServletRequest request) {
        try {
            if (apply.getTransferApplyDetailList().size() == 0) {
                throw new Exception("物料变动列表不能为空");
            }
            stockTransferService.addTransfer(apply, request);
        } catch (Exception e) {
            logger.error("stock transfer add apply stockTransferApplyDTO:{}", apply);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("添加成功", true);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "stockTransferRequestDTO", value = "", dataType = "com.jumi.microservice.stock.domain.StockTransferRequestDTO"),
    })
    @ApiOperation("分页查询调货列表")
    @GetMapping("/getListByPage")
    public TableDataInfo<List<StockTransferVO>> getTransferGoodsListByPage(StockTransferSearchDTO stockTransferSearchDTO) {
        startPage();
        List<StockTransferVO> stockTransferList = stockTransferService.getTransferList(stockTransferSearchDTO);
        return getDataTable(stockTransferList);
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "transferId", value = "申请调货记录的id", dataType = "java.lang.Long")})
    @ApiOperation("根据申请id查询调货详情")
    @GetMapping("/detail")
    public ResponseResult<StockTransferDetailVO> getTransferDetail(Long transferId) {
        try {
            StockTransferDetailVO stockTransferDetailVO = stockTransferService.getTransferDetail(transferId);
            return ResponseResult.success(stockTransferDetailVO);
        } catch (Exception e) {
            return ResponseResult.error(e.getMessage());
        }
    }

    @ApiOperation("编辑调货申请")
    @PostMapping("/edit")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockTransferApplyDTO", value = "编辑参数", dataType = "com.jumi.microservice.stock.domain.StockTransferApplyDTO")})
    @WebLog(description = "编辑调货申请")
    public ResponseResult editTransfer(@Validated @RequestBody StockTransferApplyDTO apply, HttpServletRequest request) {
        try {
            if (apply.getTransferApplyDetailList().size() == 0) {
                throw new Exception("物料变动列表不能为空");
            }
            stockTransferService.editTransfer(apply, request);
        } catch (Exception e) {
            logger.error("stock transfer edit apply stockTransferApplyDTO:{}", apply);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("编辑成功", true);
    }

    @ApiOperation("审核操作")
    @PostMapping("/audit")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "StockTransferAuditDTO", value = "审核参数", dataType = "com.jumi.microservice.stock.domain.StockTransferAuditDTO")})
    @WebLog(description = "调货审核操作")
    public ResponseResult<Boolean> auditTransfer(@Validated @RequestBody StockTransferAuditDTO auditDTO, HttpServletRequest request) {
        try {
            if (auditDTO.getTransferIds().size() == 0) {
                throw new Exception("参数不能为空");
            }
            stockTransferService.auditTransfer(auditDTO, request);
        } catch (Exception e) {
            logger.error("stock transfer audit StockTransferAuditDTO:{}", auditDTO);
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success("操作成功", true);
    }
}
